﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using LRT.Syndication.Components;
using Dapper;

namespace LRT.Syndication.Data
{
    internal partial class DataProvider
    {


        #region Item
        ///<summary>
        ///插入 文章
        ///</summary>
        public void InsertItem(Item value)
        {
            const string sql = @"INSERT INTO [Syndication].[Item] ([Title],[Link],[Description],[Author],[Category],[Comments],[Enclosure],[Guid],[PubDate],[Source])VALUES(@Title,@Link,@Description,@Author,@Category,@Comments,@Enclosure,@Guid,@PubDate,@Source)";
            using (var connection = GetDbConnection())
            {
                connection.Execute(sql, value);
            }
        }

        ///<summary>
        ///更新 文章
        ///</summary>
        public void UpdateItem(Item value)
        {

            const string sql = @"UPDATE  [Syndication].[Item] SET [Title]=@Title,[Link]=@Link,[Description]=@Description,[Author]=@Author,[Category]=@Category,[Comments]=@Comments,[Enclosure]=@Enclosure,[Guid]=@Guid,[PubDate]=@PubDate,[Source]=@Source WHERE [ID]=@ID ";
            using (var connection = GetDbConnection())
            {
                connection.Execute(sql, value);
            }
        }

        ///<summary>
        ///查找 文章
        ///</summary>
        public Item FindItem(int id)
        {
            const string sql = @"SELECT * FROM  [Syndication].[Item]  WHERE [ID]=@ID";
            using (var connection = GetDbConnection())
            {
                return connection.Query<Item>(sql, new { ID = id }).SingleOrDefault();
            }
        }

        ///<summary>
        ///删除 文章
        ///</summary>
        public void DeleteItem(int id)
        {
            const string sql = @"DELETE FROM  [Syndication].[Item]  WHERE [ID]=@ID ";
            using (var connection = GetDbConnection())
            {
                connection.Execute(sql, new { ID = id });
            }
        }

        /// <summary>
        ///  分页查询 文章
        /// </summary>
        public IEnumerable<Item> QueryItem(out int total, int pageIndex, int pageSize, object param)
        {
            const string sql = @"SELECT * FROM  [Syndication].[Item]"; //  WHERE cdt ...
            const string sort = "ID DESC";
            return GetPagedList<Item>(out total, pageIndex, pageSize, sql, sort, param);

        }

        /// <summary>
        ///  查询 文章
        /// </summary>
        public IEnumerable<Item> QueryItem(string key = null)
        {
            
            const string sql = @"SELECT * FROM  [Syndication].[Item]"; //  WHERE cdt ...
            const string sort = "ID DESC";
            return GetQueryList<Item>(sql, sort, null);
        }

        #endregion
    }
}
